package old;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import domain.Contact;


public class DAOContact {

	private final static String RESOURCE_JDBC = "java:comp/env/jdbc/jee";

	public boolean addContact(
			final Long id,
			final String firstName, 
			final String lastName, 
			final String email) {

		try {

			
			final Context lContext = new InitialContext();
			final DataSource lDataSource = (DataSource) lContext.lookup(RESOURCE_JDBC);
			final Connection lConnection = lDataSource.getConnection();

			// adding a new contact
			final PreparedStatement lPreparedStatementCreation = 
					lConnection.prepareStatement(
							"INSERT INTO contact ( "
									+ "ID_CONTACT, "
									+ "FIRSTNAME, "
									+ "LASTNAME, "
									+ "EMAIL) "
									+ "VALUES(?, ?, ?, ?)");

			lPreparedStatementCreation.setLong(1, id);
			lPreparedStatementCreation.setString(2, firstName);
			lPreparedStatementCreation.setString(3, lastName);
			lPreparedStatementCreation.setString(4, email);

			lPreparedStatementCreation.executeUpdate();
			lConnection.close();
			return true;

		} catch (NamingException e) {
			return false;
		} catch (SQLException e) {
			return false;
		}
	}

	
	
	public String updateContact(
			final Long id,
			final String firstName, 
			final String lastName, 
			final String email) {

		try {

			final Context lContext = new InitialContext();
			final DataSource lDataSource = (DataSource) lContext.lookup(RESOURCE_JDBC);
			final Connection lConnection = lDataSource.getConnection();

			// adding a new contact
			final PreparedStatement lPreparedStatementCreation = 
					lConnection.prepareStatement(
							"Update contact set "
									+ "contact.FIRSTNAME = ? "
									+ "contact.LASTNAME = ? "
									+ "contact.EMAIL = ?  "
									+ "where contact.ID_CONTACT = ?");

			lPreparedStatementCreation.setString(1, firstName);
			lPreparedStatementCreation.setString(2, lastName);
			lPreparedStatementCreation.setString(3, email);
			lPreparedStatementCreation.setLong(4, id);

			lPreparedStatementCreation.executeUpdate();
			lConnection.close();
			return null;

		} catch (NamingException e) {
			return "NamingException : " + e.getMessage();
		} catch (SQLException e) {
			return "SQLException : " + e.getMessage();
		}
	}
	
	public boolean removeContact(
			final long id) {

		try {

			final Context lContext = new InitialContext();
			final DataSource lDataSource = (DataSource) lContext.lookup(RESOURCE_JDBC);
			final Connection lConnection = lDataSource.getConnection();

			// adding a new contact
			final PreparedStatement lPreparedStatementCreation = 
					lConnection.prepareStatement(
							"Delete From contact where "
									+ "contact.ID_CONTACT = ? ");

			lPreparedStatementCreation.setLong(1, id);

			lPreparedStatementCreation.execute();
			lConnection.close();
			return true;

		} catch (NamingException e) {
			return false;
		} catch (SQLException e) {
			return false;
		}
	}
	
	public Contact searchContact(
			final Long id) {

		try {

			final Context lContext = new InitialContext();
			final DataSource lDataSource = (DataSource) lContext.lookup(RESOURCE_JDBC);
			final Connection lConnection = lDataSource.getConnection();

			// adding a new contact
			final PreparedStatement lPreparedStatementCreation = 
					lConnection.prepareStatement(
							"select * fom contact where contact.ID_CONTACT = ?");

			lPreparedStatementCreation.setLong(1, id);

			
			ResultSet res = lPreparedStatementCreation.executeQuery();
			Contact c = null;
			if(res.next()){
				c = new Contact();
				c.setId(res.getInt(1));
				c.setFirstName(res.getString(2));
				c.setLastName(res.getString(3));
				c.setEmail(res.getString(4));
			}
			
			lConnection.close();
			return c;

		} catch (NamingException e) {
			return null;
		} catch (SQLException e) {
			return null;
		}
	}
}
